/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.customer.potentialcall.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import cn.knet.sp.ncrm.common.beanvalidator.BeanValidators;
import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.persistence.Page;
import cn.knet.sp.ncrm.common.utils.DateUtils;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.potentialcall.entity.PotentialCall;
import cn.knet.sp.ncrm.modules.customer.potentialcall.service.PotentialCallService;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * 潜在客户呼叫记录Controller
 * @author zhangbin
 * @version 2015-09-01
 */
@Controller
@RequestMapping(value = "${adminPath}/customer/potentialCall")
public class PotentialCallController extends BaseController {

	@Autowired
	private PotentialCallService potentialCallService;
	
	@ModelAttribute
	public PotentialCall get(@RequestParam(required=false) String id) {
		PotentialCall entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = potentialCallService.get(id);
		}
		if (entity == null){
			entity = new PotentialCall();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"list", ""})
	public String list( HttpServletRequest request, HttpServletResponse response, Model model) {
		return "modules/customer/potentialcall/potentialCallAjaxList";//ajax模式
	}

	//@RequiresPermissions("user")
	@RequestMapping(value = "listCallData")
	@ResponseBody
	public Page<PotentialCall> listAjaxData(PotentialCall potentialCall) {
		Page<PotentialCall> page = new Page<PotentialCall>(); //不做分页但使用条件
		page.setList(potentialCallService.findList(potentialCall));
		return page;
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public Object save(PotentialCall potentialCall) {
		AjaxMessage result = AjaxMessage.success();
		try{
			potentialCall.setId(null);//保证为新记录 此处仅存储
			potentialCall.setFromUserId(UserUtils.getUser().getId());//设置用户
			BeanValidators.validateWithException(validator, potentialCall);
			potentialCall.setStartDate(DateUtils.parseDate(potentialCall.getStrStartDate(), "yyyyMMddHHmmss"));
			potentialCall.setEndDate(DateUtils.parseDate(potentialCall.getStrEndDate(), "yyyyMMddHHmmss"));
			potentialCall.setCallTime(DateUtils.getDistance(potentialCall.getStartDate(),potentialCall.getEndDate(),DateUtils.MILLIS_PER_SECOND));
			potentialCall.setDateCreated(new java.util.Date());
			potentialCall.setLastUpdated(new java.util.Date());
			potentialCallService.save(potentialCall);
			result.setMessage("保存潜在客户呼叫记录成功");
		}catch(ConstraintViolationException ex){
			List<String> list = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");
			result.makeError(list.toArray().toString());
		}catch (Exception e) {
			logger.warn("添加潜在客户呼叫记录失败", e);
			result.makeError("添加潜在客户呼叫记录失败!系统发生异常");
		}
		return result;
	}

	//ajax模式 删除
	/*
	@RequiresPermissions("customer:potentialCall:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public Object delete(PotentialCall potentialCall, RedirectAttributes redirectAttributes) {
		AjaxMessage result = AjaxMessage.success();
		try {
			potentialCallService.delete(potentialCall);
			result.appendMessage("删除潜在客户呼叫记录成功!");
		} catch (Exception e) {
			logger.warn("删除潜在客户呼叫记录失败", e);
			result.makeError("删除潜在客户呼叫记录失败!系统发生异常");
		}
		return result;
	}*/


}